/**
 * Created by root on 14-12-10.
 */

define(['angular', 'commonJS/ng-require', 'personalJS/ng-require', 'lessonJS/ng-require', 'globalNotificationJS/ng-require', 'personalMessageJS/ng-require', 'lessonDiscussionJS/ng-require', 'angularUIRouter'],
  function(angular, commonModule, personalModule, globalNotificationModule, personalMessageModule, lessonModule, lessonDiscussionModule){

    var app = angular.module('InterflowAfterClass', [commonModule, personalModule, lessonModule, globalNotificationModule, personalMessageModule, lessonDiscussionModule, 'ui.router']);

    app.constant('baseUrl', 'ngApp/');

    app.run(['$rootScope', 'MainTopNavInitService', 'LoginStatusService', 'GoBackService', 'GlobalNotificationService', 'RoleStateConfigure',
      function($rootScope, MainTopNavInitService, LoginStatusService, GoBackService, GlobalNotificationService, RoleStateConfigure){

        LoginStatusService.init('loginData', function(loginSer){

          RoleStateConfigure.init(
            loginSer.get('personalInfo.role.name'),
            {
              'lesson.show.student': ['lesson.show.student+teacher']
            }
          )
        });
        $rootScope.functionNavItems = MainTopNavInitService.getFunctionNavItems();

        $rootScope.$on('$stateChangSuccess', function(event, toState){
          $rootScope.title = toState.title || 'TeamMindmap';
        });

        GoBackService.init($rootScope);
        GlobalNotificationService.init($rootScope);

      }]);

    app.config(['$httpProvider', '$stateProvider', '$urlRouterProvider', 'baseUrl', 'RoleStateConfigureProvider',
      function($httpProvider, $stateProvider, $urlRouterProvider, baseUrl, RoleStateConfigureProvider){

        $httpProvider.defaults.headers.common['X-CSRF-TOKEN'] = $('meta[name=csrf-token]').attr('content');

        //$urlRouterProvider.otherwise('/personal/profile');

        $stateProvider
          .state('personal', {
            url: '/personal',
            views:{
              '':{
                templateUrl: baseUrl + 'personal/tpls/layout.html'
              },
              'secondNav@personal':{
                template: '<module-second-nav module-name="personal" nav-items="navItems" ></module-second-nav>',
                controller: ['$scope', function($scope){
                  $scope.navItems = [
                    {label: '用户设置', uiSref: 'personal.info.profile'},
                    {label: '个人信件', uiSref: 'personal.message.list'}
                  ];
                }]
              },
              'globalNotification@personal': {
                templateUrl: baseUrl + 'globalNotification/tpls/notification-template.html'
              }
            },
            resolve: {
              personalInfo: ['PersonalService', function(PersonalService){
                return PersonalService.getInfo()
                  .then(function(resp){ return resp.data; },
                  function(resp){
                    console.error(resp.data);
                  }
                );
              }]
            },
            abstract: true
          })
          .state('personal.info', {
            url: '/info',
            views:{
              'content@personal':{
                templateUrl: baseUrl + 'personal/tpls/personal.info.html',
                controller: ['$scope', function($scope){
                  $scope.navItems = [
                    {'label': '更改资料', uiSref: 'personal.info.profile'},
                    {'label': '修改密码', uiSref: 'personal.info.password'}
                  ];
                }]
              }
            },
            abstract: true
          })
          .state('personal.info.profile', {
            url: '/profile',
            views:{
              'infoContent@personal.info':{
                templateUrl: baseUrl + 'personal/tpls/personal.info.profile.html',
                resolve: {
                  unitList: ['UnitService', function(UnitService){
                    return UnitService.accessor.get()
                      .then(function(resp){ return resp.data; },
                        function(resp){
                          console.error(resp.data);
                          alert('加载单位信息列表出错');
                        });
                  }]
                },
                controller: 'ProfileController'
              }
            }
          })
          .state('personal.info.password', {
            url: '/password',
            views:{
              'infoContent@personal.info':{
                templateUrl: baseUrl + 'personal/tpls/personal.info.password.html',
                controller: 'PasswordController'
              }
            }
          })
          .state('personal.message', {
            url: '/message',
            views: {
              'content@personal':{
                templateUrl: baseUrl + 'personalMessage/tpls/personal.message.html',
                controller: ['$scope', function($scope){
                  $scope.navItems = [
                    {label: '查看私信', uiSref: 'personal.message.list'},
                    {label: '编辑私信', uiSref: 'personal.message.creating'}
                  ];
                }]
              }
            }
          })
          .state('personal.message.list', {
            url: '/list',
            views: {
              'content@personal.message':{
                templateUrl: baseUrl + 'personalMessage/tpls/personal.message.list.html',
                resolve: {
                  messageList: ['MessageService', function(MessageService){
                    return MessageService.accessor.get()
                      .then(function(resp){ return resp.data; },
                      function(resp){
                        console.error(resp.data);
                        alert('信息列表加载失败');
                      }
                    )
                  }]
                },
                controller: 'MessageListController'
              }
            }
          })
          .state('personal.message.show', {
            url: '/show',
            views: {
              'content@personal':{
                templateUrl: baseUrl + 'personalMessage/tpls/personal.message.show.html',
                resolve: {
                  currentMessageInfo: ['MessageService', function(MessageService){
                    return MessageService.currentMessageInfo;
                  }]
                },
                controller: 'MessageShowController'
              }
            }
          })
          .state('personal.message.creating', {
            url: '/creating',
            views: {
              'content@personal.message':{
                templateUrl: baseUrl + 'personalMessage/tpls/personal.message.creating.html',
                controller: 'MessageCreatingController'
              }
            }
          })
          .state('lesson', {
            url: '/lesson',
            views: {
              '': {
                //template: '<div ui-view="container" style="height: 100%;"></div><div ui-view="globalNotification"></div>'
                templateUrl: baseUrl + 'lesson/tpls/lesson.html'
              },
              'secondNav@lesson': {
                template: '<module-second-nav module-name="lesson.list" nav-items="navItems" compare-level="3" ></module-second-nav>',
                controller: ['$scope', 'LoginStatusService', function($scope, LoginStatusService){
                  var baseNavItems = [
                    {label: '全部课程', uiSref: 'lesson.list.all'},
                    {label: '我的课程', uiSref: 'lesson.list.mine'}
                  ];
                  var userNavItems = [];
                  var userRole = LoginStatusService.get('personalInfo.role.name');
                  if( userRole == 'teacher' ){
                    userNavItems = [
                      {label: '课程点名', uiSref: 'lesson.callname.list'},
                      {label: '新建课程', uiSref: 'lesson.list.creating'}
                    ]
                  } else {
                    userNavItems = [
                      {label: '加入课程', uiSref: 'lesson.list.join'},
                      {label: '退出课程', uiSref: 'lesson.list.exit'}
                    ]
                  }
                  $scope.navItems = baseNavItems.concat( userNavItems );
                }]
              },
              'globalNotification@lesson': {
                templateUrl: baseUrl + 'globalNotification/tpls/notification-template.html'
              }
            },
            resolve: {
              currUserId: ['LoginStatusService', function(LoginStatusService){
                return LoginStatusService.get('personalInfo.id');
              }]
            },
            abstract: true
          })
          .state('lesson.list', {
            url: '/list',
            abstract: true
          })
          .state('lesson.list.all', {
            url: '/all',
            views: {
              'content@lesson': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.list.all.mine.html',
                resolve: {
                  lessonList: ['LessonService', '$http', function (LessonService, $http) {
                    return LessonService.accessor.get()
                      .then(function (resp) {
                        return resp.data;
                      }, function (resp) {
                        alert('error');
                        console.error(resp.data);
                      });
                  }],
                  tabName: function(){
                    return '全部课程';
                  }
                },
                controller: 'LessonListController'
              }
            }
          })
          .state('lesson.list.mine', {
            url: '/mine',
            views: {
              'content@lesson': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.list.all.mine.html',
                resolve: {
                  lessonList: ['currUserId', 'LessonService', function (currUserId, LessonService) {
                    return LessonService.accessor.get({
                      params: {cond: 'mine'}
                    }).then(function (resp) {
                      return resp.data;
                    }, function (resp) {
                      alert('error');
                      console.error(resp.data);
                    });
                  }],
                  tabName: function () {
                    return '我的课程';
                  }
                },
                controller: 'LessonListController'
              }
            }
          })
          .state('lesson.list.join', {
            url: '/join',
            views: {
              'content@lesson': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.list.join.html',
                resolve: {
                  lessonList: ['currUserId', 'LessonService', function (currUserId, LessonService) {
                    return LessonService.accessor.get({
                      params: {cond: 'other'}
                    }).then(function (resp) {
                      return resp.data;
                    }, function (resp) {
                      alert('error');
                      console.error(resp.data);
                    });
                  }],
                  tabName: function(){
                    return '加入课程';
                  }
                },
                controller: 'LessonListController'
              }
            }
          })
          .state('lesson.list.exit', {
            url: 'exit',
            views: {
              'content@lesson': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.list.exit.html',
                resolve: {
                  lessonList: ['currUserId', 'LessonService', function (currUserId, LessonService) {
                    return LessonService.accessor.get({
                      params: {cond: 'exit'}
                    }).then(function (resp) {
                      return resp.data;
                    }, function (resp) {
                      alert('error');
                      console.error(resp.data);
                    });
                  }],
                  tabName: function(){
                    return '退出课程';
                  }
                },
                controller: 'LessonListController'
              }
            }
          })
          .state('lesson.list.creating', {
            url: '/creating',
            views: {
              'content@lesson':{
                templateUrl: baseUrl + 'lesson/tpls/lesson.list.creating.html',
                controller: 'LessonCreatingController'
              }
            }
          })
          .state('lesson.callname', {
            url: '/callname',
            views: {
              'secondNav@lesson':{
                templateUrl: baseUrl + 'lesson/tpls/lesson.callname-secondNav.html'
              },
              'content@lesson': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.callname.html',
                controller: ['$scope', function($scope){
                  $scope.navItems = [
                    {label: '确定点名列表', uiSref: 'lesson.callname.list'},
                    {label: '新建点名列表', uiSref: 'lesson.callname.creating'}
                  ];
                }]
              }
            },
            abstract: true
          })
          .state('lesson.callname.list', {
            url: '/list',
            views: {
              'content@lesson.callname':{
                templateUrl: baseUrl + 'lesson/tpls/lesson.callname.list.html',
                resolve:{
                  'callnameList': ['LessonCallnameService', function(LessonCallnameService){
                    return LessonCallnameService.accessor.get()
                      .then(function(resp){ return resp.data; },
                        function(resp){
                          console.error(resp.data);
                          alert('load error');
                      });
                  }]
                },
                controller: 'LessonCallnameController'
              }
            }
          })
          .state('lesson.callname.show', {
            url: '/show/:callnameId',
            views: {
              'content@lesson':{
                templateUrl: baseUrl + 'lesson/tpls/lesson.callname.show.html',
                controller: 'LessonCallnameShowController'
              }
            },
            resolve: {
              currentList: ['$stateParams', 'LessonCallnameService', function($stateParams, LessonCallnameService){
                return LessonCallnameService.accessor.show($stateParams['callnameId'])
                  .then(function(resp){ return resp.data; },
                  function(resp){
                    console.error(resp.data);
                    alert('error');
                  });
              }]
            }
          })
          .state('lesson.callname.show.do', {
            url: '/:callnameMethod',
            views: {
              'content@lesson': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.callname.show.do.html',
                resolve: {
                  currentCallnameId: ['$stateParams', function($stateParams){
                    return $stateParams['callnameId'];
                  }]
                },
                controller: 'LessonCallnameDoController'
              }
            }
          })
          .state('lesson.callname.creating', {
            url: '/creating',
            views: {
              'content@lesson.callname':{
                templateUrl: baseUrl + 'lesson/tpls/lesson.callname.creating.html',
                controller: 'LessonCallnameCreatingController'
              }
            }
          })
          .state('lesson.creating', {
            url: '/creating'
          })
          .state('lesson.show', {
            url: '/show/:lessonId',
            views: {
              'container@lesson': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.show.html'
              },
              'titleAndNav@lesson.show': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.show-titleAndNav.html',
                controller: 'LessonShowController'
              }
            },
            resolve: {
              currentLessonId: ['$stateParams', function($stateParams){
                return $stateParams['lessonId'];
              }],
              currLesson: ['$stateParams', 'LessonService', function ($stateParams, LessonService) {
                var currLessonId = $stateParams['lessonId'];
                return LessonService.accessor.show(currLessonId).then(
                  function(resp){
                    return resp.data;
                  },
                  function(resp){
                    console.log('error');
                  }
                )
              }]
            },
            abstract: true
          })
          .state('lesson.show.info', {
            url: '/info',
            views: {
              'content@lesson.show': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.show.info.html',
                controller: 'LessonInfoController',
                resolve: {
                  lesson: ['currLesson', function(currLesson){
                    return currLesson;
                  }]
                }
              }
            }
          })
          .state('lesson.show.student', {
            url: '/student',
            views: {
              'content@lesson.show': {
                templateUrl: function(){
                  return baseUrl + 'lesson/tpls/'
                  + RoleStateConfigureProvider.getTemplate('lesson.show.student');
                },
                controller: 'LessonStudentController',
                resolve: {
                  studentList: ['LessonStudentService', '$stateParams', function(LessonStudentService, $stateParams){
                    var accessor = LessonStudentService.accessor;
                    accessor.setParentResourceId($stateParams);
                    return accessor.get().then(
                      function(resp){
                        return resp.data;
                      },function(resp){
                        console.log('error');
                      })
                  }]
                }
              }
            }
          })
          .state('lesson.show.discussion', {
            url: '/discussion',
            views: {
              'content@lesson.show': {
                templateUrl: baseUrl + 'lessonDiscussion/tpls/lesson.show.discussion.html',
                resolve: {
                  discussionList: ['$stateParams', 'DiscussionService', function($stateParams, DiscussionService){
                    DiscussionService.accessor['setParentResourceId']($stateParams);

                    return DiscussionService.accessor.get()
                      .then(function(resp){ return resp.data; },
                        function(resp){
                          console.error(resp);
                          alert('加载课程讨论列表出错');
                      });
                  }]
                },
                controller: 'DiscussionListController'
              }
            }
          })
          .state('lesson.show.discussion.info', {
            url: '/info/:discussionId',
            views: {
              'container@lesson': {
                templateUrl: baseUrl + 'lessonDiscussion/tpls/lesson.show.discussion.info.html',
                resolve: {
                  currentDiscussion: [ '$stateParams','DiscussionService', function($stateParams, DiscussionService){
                    DiscussionService.accessor['setParentResourceId']($stateParams);

                    return DiscussionService.accessor.show($stateParams['discussionId'])
                      .then(function(resp) { return resp.data; },
                        function(resp){
                          console.error(resp.data);
                          alert('加载讨论信息失败');
                      });
                  }]
                },
                controller: 'DiscussionInfoController'
              }
            }
          })
          .state('lesson.show.discussion.creating', {
            url: '/creating',
            views: {
              'container@lesson': {
                templateUrl: baseUrl + 'lessonDiscussion/tpls/lesson.show.discussion.creating.html',
                controller: 'DiscussionCreatingController'
              }
            }
          })
          .state('lesson.show.resource', {
            url: '/resource',
            views: {
              'content@lesson.show': {
                templateUrl: baseUrl + 'lesson/tpls/lesson.show.resource.html',
                resolve: {
                  resourceRespData: ['$stateParams', 'LessonResourceService', function($stateParams, LessonResourceService){
                    LessonResourceService.accessor['setParentResourceId']($stateParams);
                    return LessonResourceService.accessor.get()
                      .then(function(resp){ return resp.data },
                      function(resp){
                        alert('加载资源出错');
                        console.error(resp.data);
                      });
                  }],
                  resourceList: ['resourceRespData', function(resourceRespData){
                    return resourceRespData['resources'];
                  }],
                  userPrivilege: ['resourceRespData', function(resourceRespData){
                    return resourceRespData['privilege'];
                  }]
                },
                controller: 'LessonResourceController'
              }
            }
          })
          .state('lesson.show.edit', {

          })
        ;
      }]);

    return app;
  }
);

